package com.yunwaikeji.gy_tool_library.widget.recycler;

import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSnapHelper;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SnapHelper;

import com.yunwaikeji.gy_tool_library.R;
import com.yunwaikeji.gy_tool_library.info.GYBaseInfo;
import com.yunwaikeji.gy_tool_library.info.GYRecyclerInfo;
import com.yunwaikeji.gy_tool_library.utils.display.GYDisplayUtils;
import com.yunwaikeji.gy_tool_library.utils.GYRecyclerViewUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GYRecyclerBuilder {
    private final GYRecyclerView gyRecyclerView;
    private int recyclerDisplayStyle = GYRecyclerInfo.DisplayStyle.VERTICAL;
    private int dividerSizeVerticalPx = 0;
    private int dividerColorVertical = Color.TRANSPARENT;
    private int dividerSizeHorizontalPx = 0;
    private int dividerColorHorizontal = Color.TRANSPARENT;
    private int spanCount = 4;
    private int dividerColorShowUp = GYRecyclerInfo.DividerColorShowUp.VERTICAL;
    private List<Integer> dividerPositionList;
    private int position = 0;
    private boolean scrollMany = false;
    private int overScrollMode = View.OVER_SCROLL_NEVER;

    public GYRecyclerBuilder(GYRecyclerView gyRecyclerView, AttributeSet attributeSet) {
        this.gyRecyclerView = gyRecyclerView;
        String dividerPositions = null;
        int dividerStart = GYBaseInfo.MATCH_PARENT;
        int dividerEnd = GYBaseInfo.MATCH_PARENT;
        int dividerSizePx = GYBaseInfo.WRAP_CONTENT;
        int dividerColor = GYBaseInfo.WRAP_CONTENT;
        if (attributeSet != null) {
            TypedArray typedArray = gyRecyclerView.getContext().obtainStyledAttributes(attributeSet, R.styleable.GYRecyclerView);
            recyclerDisplayStyle = typedArray.getInteger(R.styleable.GYRecyclerView_recyclerDisplayStyle, recyclerDisplayStyle);
            dividerSizePx = typedArray.getDimensionPixelSize(R.styleable.GYRecyclerView_dividerSize, dividerSizePx);
            dividerColor = typedArray.getColor(R.styleable.GYRecyclerView_dividerColor, dividerColor);
            dividerSizeVerticalPx = typedArray.getDimensionPixelSize(R.styleable.GYRecyclerView_dividerSizeVertical, dividerSizeVerticalPx);
            dividerColorVertical = typedArray.getColor(R.styleable.GYRecyclerView_dividerColorVertical, dividerColorVertical);
            dividerSizeHorizontalPx = typedArray.getDimensionPixelSize(R.styleable.GYRecyclerView_dividerSizeHorizontal, dividerSizeHorizontalPx);
            dividerColorHorizontal = typedArray.getColor(R.styleable.GYRecyclerView_dividerColorHorizontal, dividerColorHorizontal);
            spanCount = typedArray.getInteger(R.styleable.GYRecyclerView_spanCount, spanCount);
            dividerPositions = typedArray.getString(R.styleable.GYRecyclerView_dividerPositions);
            dividerStart = typedArray.getInteger(R.styleable.GYRecyclerView_dividerStart, dividerStart);
            dividerEnd = typedArray.getInteger(R.styleable.GYRecyclerView_dividerEnd, dividerEnd);
            scrollMany = typedArray.getBoolean(R.styleable.GYRecyclerView_scrollMany, scrollMany);
            dividerColorShowUp = typedArray.getInteger(R.styleable.GYRecyclerView_dividerColorShowUp, dividerColorShowUp);
            overScrollMode = typedArray.getInteger(R.styleable.GYRecyclerView_android_overScrollMode, overScrollMode);
            typedArray.recycle();
        }
        if (dividerSizePx != GYBaseInfo.WRAP_CONTENT) {
            dividerSizeVerticalPx = dividerSizePx;
            dividerSizeHorizontalPx = dividerSizePx;
        }
        if (dividerColor != GYBaseInfo.WRAP_CONTENT) {
            dividerColorVertical = dividerColor;
            dividerColorHorizontal = dividerColor;
        }
        if (!TextUtils.isEmpty(dividerPositions)) {
            String[] strings = dividerPositions.split(",");
            for (String s : strings) {
                setDividerPositions(Integer.parseInt(s));
            }
        } else {
            if (dividerStart == GYBaseInfo.MATCH_PARENT && dividerEnd == GYBaseInfo.MATCH_PARENT) {
                dividerPositionList = null;
            } else {
                if (dividerStart == GYBaseInfo.MATCH_PARENT) {
                    dividerStart = 0;
                }
                if (dividerEnd == GYBaseInfo.MATCH_PARENT) {
                    dividerEnd = gyRecyclerView.getChildCount();
                }
                setDividerPosition(dividerStart, dividerEnd);
            }
        }
        gyRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                if (layoutManager instanceof LinearLayoutManager) {
                    int firs = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
                    if (position != firs) {
                        position = firs;
                        if (gyRecyclerView.getOnPagerChangeListener() != null)
                            gyRecyclerView.getOnPagerChangeListener().onPagerChange(position);
                    }
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
            }
        });
        setDividerSizeVerticalPx(dividerSizeVerticalPx);
        setDividerColorVertical(dividerColorVertical);
        setDividerSizeHorizontalPx(dividerSizeHorizontalPx);
        setDividerColorHorizontal(dividerColorHorizontal);
    }

    public GYRecyclerBuilder setDividerPositions(List<Integer> dividerPositionList) {
        this.dividerPositionList = dividerPositionList;
        return this;
    }

    public GYRecyclerBuilder setDividerPositions(Integer... dividerPositions) {
        return setDividerPositions(Arrays.asList(dividerPositions));
    }

    public GYRecyclerBuilder setDividerPosition(int dividerStart, int dividerEnd) {
        if (dividerStart <= dividerEnd) {
            dividerPositionList = new ArrayList<>();
            for (int i = dividerStart; i <= dividerEnd; i++) {
                dividerPositionList.add(i);
            }
        }
        return this;
    }

    public GYRecyclerBuilder setDividerPositionAll() {
        dividerPositionList = null;
        return this;
    }

    public int getRecyclerDisplayStyle() {
        return recyclerDisplayStyle;
    }

    public int getDividerSizeVerticalPx() {
        return dividerSizeVerticalPx;
    }

    public GYRecyclerBuilder setDividerSizeVerticalPx(int dividerSizeVerticalPx) {
        this.dividerSizeVerticalPx = dividerSizeVerticalPx;
        return this;
    }

    public GYRecyclerBuilder setDividerSizeVerticalDp(int dividerSizeVerticalDp) {
        return setDividerSizeVerticalPx(GYDisplayUtils.dp2px(gyRecyclerView.getContext(), dividerSizeVerticalDp));
    }

    public int getDividerColorVertical() {
        return dividerColorVertical;
    }

    public GYRecyclerBuilder setDividerColorVertical(int dividerColorVertical) {
        this.dividerColorVertical = dividerColorVertical;
        return this;
    }

    public int getDividerSizeHorizontalPx() {
        return dividerSizeHorizontalPx;
    }

    public GYRecyclerBuilder setDividerSizeHorizontalPx(int dividerSizeHorizontalPx) {
        this.dividerSizeHorizontalPx = dividerSizeHorizontalPx;
        return this;
    }

    public GYRecyclerBuilder setDividerSizeHorizontalDp(int dividerSizeHorizontalDp) {
        return setDividerSizeHorizontalPx(GYDisplayUtils.dp2px(gyRecyclerView.getContext(), dividerSizeHorizontalDp));
    }

    public int getDividerColorHorizontal() {
        return dividerColorHorizontal;
    }

    public void setDividerColorHorizontal(int dividerColorHorizontal) {
        this.dividerColorHorizontal = dividerColorHorizontal;
    }

    public GYRecyclerView build() {
        switch (recyclerDisplayStyle) {
            case GYRecyclerInfo.DisplayStyle.VERTICAL:
            case GYRecyclerInfo.DisplayStyle.HORIZONTAL:
            case GYRecyclerInfo.DisplayStyle.PAGER_HORIZONTAL:
            case GYRecyclerInfo.DisplayStyle.PAGER_VERTICAL:
                switch (recyclerDisplayStyle) {
                    case GYRecyclerInfo.DisplayStyle.PAGER_HORIZONTAL:
                    case GYRecyclerInfo.DisplayStyle.PAGER_VERTICAL:
                        setScrollMany(false);
                        break;
                }
                GYRecyclerViewUtils.LinearLayout linearLayout = GYRecyclerViewUtils.getInstance().linearLayout(gyRecyclerView)
                        .setDividerPositions(dividerPositionList);
                switch (recyclerDisplayStyle) {
                    case GYRecyclerInfo.DisplayStyle.VERTICAL:
                    case GYRecyclerInfo.DisplayStyle.PAGER_VERTICAL:
                        linearLayout.setDividerPx(dividerSizeVerticalPx)
                                .setDividerColor(dividerColorVertical);
                        break;
                    case GYRecyclerInfo.DisplayStyle.HORIZONTAL:
                    case GYRecyclerInfo.DisplayStyle.PAGER_HORIZONTAL:
                        linearLayout.setDividerPx(dividerSizeHorizontalPx)
                                .setDividerColor(dividerColorHorizontal);
                        break;
                }
                linearLayout.build(recyclerDisplayStyle);
                break;
            case GYRecyclerInfo.DisplayStyle.GRID:
                GYRecyclerViewUtils.getInstance().gridLayout(gyRecyclerView)
                        .setDividerVerticalPx(dividerSizeVerticalPx)
                        .setDividerVerticalColor(dividerColorVertical)
                        .setDividerHorizontalPx(dividerSizeHorizontalPx)
                        .setDividerHorizontalColor(dividerColorHorizontal)
                        .setDividerColorShowUp(dividerColorShowUp)
                        .setSpanCount(spanCount)
                        .build();
                break;
        }
        return gyRecyclerView;
    }

    public GYRecyclerBuilder setRecyclerDisplayStyle(int recyclerDisplayStyle) {
        this.recyclerDisplayStyle = recyclerDisplayStyle;
        return this;
    }

    public GYRecyclerBuilder setScrollMany(boolean scrollMany) {
        this.scrollMany = scrollMany;
        SnapHelper snapHelper;
        if (scrollMany) {
            snapHelper = new LinearSnapHelper(); //一次可滑动多个
        } else {
            snapHelper = new PagerSnapHelper();//一次滑动单个
        }
        snapHelper.attachToRecyclerView(gyRecyclerView);
        return this;
    }

    public boolean isScrollMany() {
        return scrollMany;
    }

    public GYRecyclerBuilder setDividerSizePx(int dividerSizePx) {
        setDividerSizeVerticalPx(dividerSizePx);
        setDividerSizeHorizontalPx(dividerSizePx);
        return this;
    }

    public GYRecyclerBuilder setDividerSizeDp(int dividerDp) {
        return setDividerSizePx(GYDisplayUtils.dp2px(gyRecyclerView.getContext(), dividerDp));
    }

    public GYRecyclerBuilder setDividerColor(int dividerColor) {
        setDividerColorVertical(dividerColor);
        setDividerColorHorizontal(dividerColor);
        return this;
    }

    public GYRecyclerBuilder setSpanCount(int spanCount) {
        this.spanCount = spanCount;
        return this;
    }

    public int getSpanCount() {
        return spanCount;
    }

    public int getOverScrollMode() {
        return overScrollMode;
    }
}
